package com.liuwei.msmall.order.listener;

import com.liuwei.msmall.order.entity.OrderEntity;
import com.liuwei.msmall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @Classname OrderCloseListener
 * @Description TODO
 * @Date 2020/7/23 19:02
 * @Created by 刘
 */
@RabbitListener(queues = {"order.release.order.queue"})//标明接收哪些队列的数据
@Service
public class OrderCloseListener {

    @Autowired
    private OrderService orderService;


    //接收30分钟之后还未支付的订单
    @RabbitHandler
    public void handleOrderClose(org.springframework.amqp.core.Message message, OrderEntity orderEntity, Channel channel) throws IOException {

        System.out.println("接收的3分钟后未付款的订单："+orderEntity.getOrderSn()+"时间："+ LocalDateTime.now());
        try {
            orderService.orderClose(orderEntity);
            //todo 手动进行支付收单

            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            //出现异常直接将消息重新入队
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }

}
